package com.chen.springboot.controller;




import cn.hutool.core.date.DateUtil;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.chen.springboot.common.Result;


import com.chen.springboot.mapper.CollectMapper;
import com.chen.springboot.utils.TokenUtils;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.chen.springboot.service.ICollectService;
import com.chen.springboot.entity.Collect;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Resource
    private ICollectService collectService;
    private CollectMapper collectMapper;



    // 添加新的收藏
    @PostMapping
    public Result save(@RequestBody Collect collect) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        List<Collect> list = collectService.list(queryWrapper);

        collect.setUserId(TokenUtils.getCurrentUser().getId());//填入当前登录用户的id  ，根据登录用户id来显示收藏标题
        collect.setTime(DateUtil.now());  // new Date() 获取当前收藏时间
        return Result.success(collectService.save(collect));
    }


    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        collectService.removeById(id);
        return Result.success();
    }

    //删除所有
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        collectService.removeByIds(ids);
        return Result.success();
    }

    //查询所有
    @GetMapping
    public Result findAll() {
        Integer userId = TokenUtils.getCurrentUser().getId();
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return Result.success(collectService.list(queryWrapper));
    }

    //按照id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(collectService.getById(id));
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(     @RequestParam (defaultValue = "") String title,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (StrUtil.isNotBlank(title)) {         //模糊查询
            queryWrapper.like("title", title);   ///属性，值
        }
        Integer userId = TokenUtils.getCurrentUser().getId();
        queryWrapper.eq("user_id",userId);

        return Result.success(collectService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    //分页查询
    @GetMapping("/pageAdmin")
    public Result findPageAdmin(@RequestParam (defaultValue = "") String title,
                                @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (StrUtil.isNotBlank(title)) {         //模糊查询
            queryWrapper.like("title", title);   ///属性，值
        }

        return Result.success(collectService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }




}

